import 'package:flutter/material.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/localization/default_localizations.dart';
import 'package:flutterlib/common/style/meterial_design_icons.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/common/utils/navigator_utils.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/pages/interest/viewmodel/interest_store.dart';
import 'package:flutterlib/pages/post/admin/post_list_normal_widget_page.dart';
import 'package:flutterlib/widgets/quhui_tabbar_widget.dart';
import 'package:flutterlib/widgets/round_underline_tabindicator.dart';
import 'package:thrio/thrio.dart';

import 'activity_list_normal_widget_page.dart';

//用于加载内部继承上下拉的页面state的外部容器页面，
//适用于带tabbar，内部是list的页面的改造
class Page extends StatefulWidget {
  Page({Key key}) : super(key: key);

  @override
  PageState createState() => PageState();
}

class PageState extends State<Page>
    with SingleTickerProviderStateMixin {
  final SettingsStore store =
      Provider.of<SettingsStore>(currentContext, listen: false);
  InterestStore interestStore = InterestStore();
  int _tabIndex = 0;
  List<Widget> tabPageView = new List();
  List<Widget> tabViews = new List();
  Map<int, PostNormalListWidgetPage> pages = {};

  @override
  void initState() {
    super.initState();
    trueDebugPrint("fluttermobx postpage init");

    tabPageView.add(new PostNormalListWidgetPage(
      name: "文章",
      interestId: 0,
      type: 0,
    ));
    tabViews.add(renderItem("文章", 0));

    tabPageView.add(new PostNormalListWidgetPage(
      name: "趣图",
      interestId: 0,
      type: 1,
    ));
    tabViews.add(renderItem("趣图", 0));

    tabPageView.add(new ActivityNormalListWidgetPage(
      name: "招募",
      interestId: 0,
      type: 2,
    ));
    tabViews.add(renderItem("招募", 0));

    tabPageView.add(new ActivityNormalListWidgetPage(
      name: "兴趣",
      interestId: 0,
      type: 3,
    ));
    tabViews.add(renderItem("兴趣", 0));
    tabPageView.add(new PostNormalListWidgetPage(
      name: "反馈",
      interestId: 0,
      type: 4,
    ));
    tabViews.add(renderItem("反馈", 0));
  }

  renderItem(String item, int i) {
    return VgTab(
      child: new Text(
        item,
        style: QuhuiConstant.smallSubText,
        maxLines: 1,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    print(context.widget.toStringShort());


      ///绘制顶部 tab 控件
      return new QuhuiTabBarWidget(
        key: ValueKey(context.widget.toStringShort()),
        type: TabType.top,
        tabBarType: 1,
        onMorePress: () {
          ThrioNavigator.push(
              poppedResult: (result) {
                setState(() {
                  trueDebugPrint("sortinterestpage back + ${result.toString()}",
                      );
                });
              },
              url: '/interest/sort');
          // NavigatorUtils.NavigatorRouter(context, SortInterestPage());
        },
        tabItems: tabViews,
        resizeToAvoidBottomPadding: false,
        tabViews: tabPageView,
        backgroundColor: QuhuiColors.white,
        indicatorColor: QuhuiColors.mainThemeColor,
        // floatingActionButton: FloatingActionButton(
        //     child: Icon(MIcon.add),
        //     onPressed: () {
        //       if (isLogin) {
        //         NavigatorUtils.gotoActionSelectPage(context);
        //       } else {
        //         NavigatorUtils.goLogin(context);
        //       }
        //     }),
        title: Text('管理功能', style: QuhuiConstant.normalTextBold),
        leading: Material(
          color: Colors.transparent,
          child: InkWell(
            borderRadius: const BorderRadius.all(
              Radius.circular(32.0),
            ),
            onTap: () {
              ThrioNavigator.pop();
            },
            child: Icon(
              Icons.arrow_back,
              color: Colors.black,
            ),
          ),
        ),
        onPageChanged: (index) {
          _tabIndex = index;
        },
      );
  }

  Widget getSearchBarUI() {
    return Flex(
      direction: Axis.horizontal,
      children: <Widget>[
        Expanded(
          flex: 5,
          child: Container(
            margin: EdgeInsets.only(right: 10, left: 10),
            decoration: BoxDecoration(
              color: QuhuiColors.grayColor,
              borderRadius: BorderRadius.circular(20),
            ),
            child: Row(
              children: [
                SizedBox(
                  width: 25,
                  height: 25,
                  child: Icon(
                    MIcon.search,
                    size: 14,
                    color: Color(0xFF999999),
                  ),
                ),
                InkWell(
                  onTap: (){
                    NavigatorUtils.gotoPostSearch(context, '');
                  },
                  child: Text(
                    QuhuiLocalizations.i18n(context).search_hint,
                    style: TextStyle(
                      fontSize: 12,
                      color: QuhuiColors.subGrayColor,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),

      ],
    );
  }

  int getInterestId() {
    if (_tabIndex == 0) {
      return 0;
    }
    var interests = store.interests;
    return interests[_tabIndex - 1]?.id;
  }
}
